package com.hurui.xiaoming.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.http.ApiClient;
import com.hurui.xiaoming.http.ApiSevice;
import com.hurui.xiaoming.utils.DateUtil;
import com.hurui.xiaoming.weight.ProgressTips;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class BaseActivity extends AppCompatActivity implements DialogInterface.OnCancelListener {
    public static final int POST = 0 ;
    public static final int GET  = 1 ;

    public Context mContext ;

    private ProgressTips mExecProgress;

    public ApiSevice apiSevice = ApiClient.getModel().getService();

    public SystemBarTintManager tintManager;

    @Override
    protected void onCreate(Bundle arg0) {

        super.onCreate(arg0);
        mContext = getApplicationContext() ;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        MIUISetStatusBarLightMode(getWindow(), true);
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.systembartintColor2);// 通知栏所需颜色

    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);// 状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);// 清除黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }


    public abstract void initView( );

    public abstract void initAction() ;


    public void onOpen(Intent intent) {
        this.startActivity(intent);
    }

    public void onOpenForResult(Intent intent, Integer requestCode) {
        this.startActivityForResult(intent, requestCode);
    }

    public void onFinish() {
        finish();

    }

    public void onFinishForResult(Intent intent,Integer resultCode) {
        this.setResult(resultCode, intent);
        onFinish();
    }


    public void netAction(String url , Map<String , String> parmas , int type){
        Call<Object> call = null ;
        switch (type){
            case GET:
                call = apiSevice.onHttpGetOption(url , parmas);
                break;
            case POST:
                call = apiSevice.onHttpPostOption(url , parmas);
                break;
            default:
                break;
        }

        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                if(response.isSuccessful()){
                    Object object = response.body() ;
                    if(object != null){
                        onHttpSuccess(object);
                    }else{
                        onHttpFail(response.code() , "body为null");
                    }
                }else{
                    onHttpFail(response.code() , "response不成功");
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
            }
        });
    }


    public void onHttpSuccess(Object object){};

    public void onHttpFail(int code , String msg){};


    public void onShowPd() {
        if(mExecProgress==null){
            mExecProgress = ProgressTips.show(this,this);
        }
    }

    public void onHidePd() {
        if(mExecProgress!=null){
            mExecProgress.dismiss();
            mExecProgress=null;
        }
    }

    public void onToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        return;
    }


    @Override
    public void onCancel(DialogInterface dialog) {
        onHidePd();
    }
    //数据检测,监测是否为空
    public boolean checkData(EditText e){
        if("".equals(e.getText())){
            return  false;
        }
        return true;
    }

    //密码对比
    public boolean comparePass(EditText e1, EditText e2){
        if(e1.equals(e2)){
            return true;
        }
        return false;
    }

    //判断用户是否登录过期
    public boolean isStop(int lastTime){
        int nowTime = DateUtil.getSecondTimestamp();
        if( (nowTime - lastTime) / 60 >= 30){
            return true;
        }
        return false;
    }


}
